package sis.ra.pattern.pmi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import sis.ra.partofspeech.wordindex;
import sis.ra.utility.Utils;
import sis.ra.yahoor.result.extractPattern;
import sis.ra.yahoor.result.parseYahooResults;

public class pmipattern {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String file="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verb";
		String content=Utils.readFile(file);
		
		String [] lines=content.split("\n");
		int compprodpair=0;
		HashMap<String, Integer> verbxy=new HashMap<String, Integer> ();
		HashMap<String, Integer> verbs=new HashMap<String, Integer> ();
		HashMap<String, Integer> verbcompprod=new HashMap<String, Integer> ();
		HashMap<String, pmi> verbcp=new HashMap<String, pmi> ();
		String companyproduct="";
		for (int i=0;i<lines.length;i++)
		{
			String line=lines[i];
			if (line.length()<2) continue;
			if (line.startsWith("==>")) continue;
			if (lines[i].startsWith("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo result_1006\\"))
			{
				companyproduct=lines[i].replace("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo result_1006\\", "").replace(".txt", "").trim();
			}else 
			{
	//			System.out.println(line);
				String verb=line.split("\t")[1];
				String stemed=extractPattern.stem(verb);
				if (verbxy.keySet().contains(stemed+"\t"+companyproduct))
				{
					Integer count=verbxy.get(stemed+"\t"+companyproduct)+1;
					verbxy.remove(stemed+"\t"+companyproduct);
					verbxy.put(stemed+"\t"+companyproduct,count);
	//				System.out.println(stemed+"\t"+companyproduct+"\t"+count);
				}
				else
				{
					verbxy.put(stemed+"\t"+companyproduct, 1);
				}		
			}					
		}
		
		file="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\result1006.verbstat";
		content=Utils.readFile(file);
		
		lines=content.split("\n");
		for (int i=0;i<lines.length;i++)
		{
			if (lines[i].startsWith("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo result_1006\\"))
			{
				companyproduct=lines[i].replace("C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo result_1006\\", "").replace(".txt", "").trim();
			}else if (lines[i].startsWith("verb"))
			{
				String line=lines[i].substring(5).trim();
				String verb=line.split("\t")[0];
				Integer count=Integer.parseInt(line.split("\t")[1]);
				verbs.put(verb+"\t"+companyproduct, count);		
		//		System.out.println(verb+"\t"+companyproduct+"\t"+ count);
			}else if (lines[i].startsWith("Company Product Pair:"))
			{
				Integer count=Integer.parseInt(lines[i].substring(21).trim());
				verbcompprod.put(companyproduct, count);		
			}
		}
		
		HashMap<String, String> verbpmi=new HashMap<String, String>();
		
		double maxpmi=0;
		Iterator<String> it=verbxy.keySet().iterator();
		while (it.hasNext())
		{
			String term=it.next();
			String verb=term.split("\t")[0];
			String cp=term.split("\t")[1];
			Integer count1=verbxy.get(term);
			Integer count2=1;
			if (verbs.keySet().contains(term))
				count2=verbs.get(term);
			Integer count3=verbcompprod.get(term.split("\t")[1]);
			double pmiscore=(double)count1/(count2*count3);
			if (maxpmi<pmiscore)
				maxpmi=pmiscore;
			System.out.println(term+"\t"+count1+"\t"+count2+"\t"+count3+"\t"+pmiscore);
			//pmi(String companyproduct, int cpfreq, double pmi, String verb, int verbcpfreq, int verbfreq) {
			pmi pmiitem=new pmi(cp,count3,pmiscore,verb,count1,count2);
			verbcp.put(term, pmiitem);
			
			if (verbpmi.containsKey(verb))
			{

				String s=verbpmi.get(verb);
				int num=Integer.parseInt(s.split("\t")[0])+1;
				double score=Double.parseDouble(s.split("\t")[1])+pmiscore;
				verbpmi.remove(verb);
				verbpmi.put(verb, num+"\t"+score);
				
			}else
			{
				verbpmi.put(verb, 1+"\t"+pmiscore);
			}			
		}
		
		Iterator<String> its=verbpmi.keySet().iterator();
		while (its.hasNext())
		{
			String verb=its.next();
			System.out.println(verb+"\t"+ verbpmi.get(verb));
		}
	}

}
